package com.chj.comp.common.rabbitmq.sender;

import com.alibaba.fastjson.JSON;
import com.chj.comp.common.rabbitmq.core.AbstractSender;
import com.chj.comp.common.rabbitmq.message.MessageEntity;
import com.chj.comp.common.rabbitmq.queue.MqTopicQueue;
import com.chj.comp.common.rabbitmq.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/**
 *
  * @Description: 发布订阅模型-主题类型消息发送
  * @author cuihui
  * @date 2022-06-13
  *
 */
@Slf4j
public class MqTopicSender extends AbstractSender<Message> {

    public MqTopicSender(RabbitTemplate rabbitTemplate) {
        super(rabbitTemplate);
    }

    @Override
    public String send(Message message) {
        // 发送消息：消息发送至指定路由
        MessageEntity messageEntity = JSON.parseObject(message.getBody(), MessageEntity.class);
        if(messageEntity.getName().contains("1")){
            // 两个队列都能匹配
            rabbitTemplate.convertAndSend(MqTopicQueue.EXCHANGE_NAME, "mq.routing.key", message);
        }else{
            // 只能匹配到队列2
            rabbitTemplate.convertAndSend(MqTopicQueue.EXCHANGE_NAME, "mq.routing.test.key", message);
        }
        return message.getMessageProperties().getMessageId();
    }

}
